<?php


namespace App\Model;


use App\Tool\Price;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Model\GoodsSku
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property float $price 价格
 * @property int $num 库存量
 * @property int $goods_id 商品id
 * @property string $img 商品规格图片，一张
 * @property-read mixed $number
 * @property-read mixed $stock
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Model\GoodsSkuDetail[] $goods_sku_detail
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \App\Model\Goods $goods
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read int|null $goods_sku_detail_count
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\GoodsSku onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\GoodsSku whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\GoodsSku withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\GoodsSku withoutTrashed()
 * @property-read mixed $real_price
 * @property-read array $sku_info_array
 * @property-read string $sku_info
 * @property-read mixed $snapped_end_time
 * @property-read mixed $snapped_price
 * @property-read \App\Model\SnappedSku|null $snapped_sku
 */
class GoodsSku extends Base
{
    use SoftDeletes;

    protected $table = 'goods_sku';

    protected $fillable = ['price', 'num', 'goods_id', 'img'];

    protected $appends = ['number', 'stock', 'sku_info', 'sku_info_array', 'snapped_price', 'snapped_end_time', 'real_price'];

    function goods_sku_detail()
    {

        return $this->hasMany(GoodsSkuDetail::class, 'goods_sku_id', 'id');
    }


    function goods()
    {


        return $this->belongsTo(Goods::class, 'goods_id', 'id');
    }

    /**
     * 商品编号
     * Create by Peter
     * 2020/06/01 10:02:33
     * Email:904801074@qq.com
     * @return string
     */
    function getNumberAttribute()
    {
        $id = $this->attributes['id'];

        $time = strtotime($this->attributes['created_at']);

        return $time . $id;
    }

    /**
     * 库存强制转换为整型(用于库存sku选择判断,也可以用做真实的库存)
     * Create by Peter
     * 2020/06/01 10:02:49
     * Email:904801074@qq.com
     * @return string
     */
    function getStockAttribute()
    {

        try {

            $snapped_sku = $this->getRelation('snapped_sku');

            $snapped = $snapped_sku->snapped;

            $is_time = $snapped->is_time;


            if($is_time) return (string)$snapped_sku->num;

            return (string)$this->attributes['num'];

        } catch (\Exception $exception) {

            return (string)$this->attributes['num'];

        }


    }

    /**
     * 获取sku信息，例：甜味，100寸
     * Create by Peter
     * 2020/06/01 10:07:46
     * Email:904801074@qq.com
     * @return string
     */
    function getSkuInfoAttribute()
    {


        $data = array_key_exists('goods_sku_detail', $this->getRelations()) ? $this->getRelations()['goods_sku_detail'] : [];

//        dd($data);

        if (!$data) return '';

        $info = '';

        foreach ($data as $key => $v) {

            if ($key != 0) $info .= ',';

            $info .= $v['value'];


        }

        return $info;

    }


    /**
     * 获取sku信息，上面那个的数组形式
     * Create by Peter
     * 2020/06/01 10:07:46
     * Email:904801074@qq.com
     * @return array
     */
    function getSkuInfoArrayAttribute()
    {

        $data = array_key_exists('goods_sku_detail', $this->getRelations()) ? $this->getRelations()['goods_sku_detail'] : [];

        if (!$data) return [];

        $info = [];

        foreach ($data as $key => $v) {

            $tem = ['key' => $v['key'], 'value' => $v['value']];

            $info[] = $tem;


        }

        return $info;

    }

    function snapped_sku()
    {


        return $this->hasOne(SnappedSku::class, 'sku_id', 'id');
    }


    /**
     * 获取秒杀价格
     * Create by PeterYang
     * 2020/06/08 23:06:23
     * Email:904801074@qq.com
     */
    function getSnappedPriceAttribute()
    {

        try {

            $relations = $this->getRelation('snapped_sku');

            if (!$relations) return false;

            //秒杀价格数组
            $snapped_sku = $relations->toArray();


            $price = false;

            $type = $snapped_sku['type'];

            //固定价格
            if ($type == 1) $price = $snapped_sku['price'];

            //折扣
            if ($type == 2) $price = number_format($this->attributes['price'] * ($snapped_sku['percent'] / 10), 2);


            //秒杀时间
            $snapped = $snapped_sku['snapped'];

            if ($snapped['is_time']) return $price;


            return false;


        } catch (\Exception $exception) {

//            dd($exception->getMessage());

            return false;

        }


    }


    function getRealPriceAttribute()
    {

        try {

            $relations = $this->getRelation('snapped_sku');

            if (!$relations) return $this->attributes['price'];

            //秒杀价格数组
            $snapped_sku = $relations->toArray();


            $price = false;

            $type = $snapped_sku['type'];

            //固定价格
            if ($type == 1) $price = $snapped_sku['price'];

            //折扣
            if ($type == 2) $price = number_format($this->attributes['price'] * ($snapped_sku['percent'] / 10), 2);


            //秒杀时间
            $snapped = $snapped_sku['snapped'];


            $is_time = $snapped['is_time'];

            if ($is_time) return $price;

            return $this->attributes['price'];


        } catch (\Exception $exception) {


            return $this->attributes['price'];

        }

    }

    /**
     * 秒杀时间(单位：秒)
     * Create by Peter
     * 2020/06/09 11:06:43
     * Email:904801074@qq.com
     */
    function getSnappedEndTimeAttribute()
    {


        try {

            $relations = $this->getRelation('snapped_sku');

            if (!$relations) return false;

            //秒杀价格数组
            $snapped_sku = $relations->toArray();


            //秒杀时间
            $snapped = $snapped_sku['snapped'];

            $is_time = $snapped['is_time'];

            if (!$is_time) return false;

//            $time = 0;

//            Price::isTime($snapped, function ($snapped) use (&$time) {

//                $time
            $cycle = $snapped['cycle'];

            switch ($cycle) {

                case 1:

                    $end_time = strtotime($snapped['end_time']);

                    return $end_time - time();

                    break;

                case 2:

                    $nows = strtotime("2020-01-23 " . date('H:i:s', time()));

                    $every_day_end_time = strtotime($snapped['every_day_end_time']);

                    return $every_day_end_time - $nows;

                    break;


                case 3:

                    return strtotime('23:59:59') - time();

                    break;

                case 4:

                    //
                    return strtotime('23:59:59') - time();

                    break;

            }

//                return $time;

//            });

//            return $time;

//            $cycle = $snapped['cycle'];
//
//
//            $now = time();
//
//            switch ($cycle) {
//
//                //一次
//                case 1:
//
//
//                    $start_time = strtotime($snapped['start_time']);
//
//                    $end_time = strtotime($snapped['end_time']);
//
//
//                    if ($now > $start_time && $now < $end_time){
//
//
//                        return $end_time-$now;
//
//                    }
//
//                    return false;
//
//
//                    break;
//
//                //每天
//                case 2:
//
//
//                    //转换成同一天
//                    $every_day_start_time = strtotime(date('H:i:s', strtotime($snapped['every_day_start_time'])));
//
//                    $every_day_end_time = strtotime(date('H:i:s', strtotime($snapped['every_day_end_time'])));
//
//                    if ($now > $every_day_start_time && $now < $every_day_end_time){
//
//
//                        return $every_day_end_time-$now;
//
//                    }
//
//                    return false;
//
//
//                    break;
//
//                //每周
//                case 3:
//
//                    $week_time_array=$snapped['week_time'];
//
//                    $week=(int)date("w",$now);
//
//                    if($week==0) $week=7;
//
//                    if(in_array($week,$week_time_array)){
//
//
//                        return strtotime('23:59:59')-$now;
//
//                    }
//
//                    return  false;
//
//
//
//                    break;
//
//
//                //每月
//                case 4:
//
//                    $date_time_array=$snapped['date_time'];
//
//                    $day=date("j",$now);
//
//                    if(in_array($day,$date_time_array)){
//
//                        return strtotime('23:59:59')-$now;
//
//                    }
//
//                    return false;
//
//                    break;
//
//
//                default:
//
//                    return false;
//
//
//            }


        } catch (\Exception $exception) {


            return false;

        }


    }

}
